<? include "connection.php";
    function vyhodnot($cislo){
      switch ($cislo) {
        case ($cislo < 18.5): return ("<div id='warning'> Podvaha</div>");
          break;
        case ($cislo >= 18.5 && $cislo < 24.9): return ("<div> Normalni</div>");
          break;
        case ($cislo >= 24.9 && $cislo < 29.9): return ("<div id='warning'>Nadvaha</div>");
          break;
        case ($cislo >= 29.9 && $cislo < 34.9): return ("<div id='warning'>Obezita 1. stupne</div>");
          break;
	case ($cislo >= 34.9 && $cislo < 39.9): return ("<div id='warning'>Obezita 2. stupne</div>");
          break;
	case ($cislo >= 39.9): return ("Obezita 3. stupne");
          break;
      }
    }
    function validateHeight($height){
      if($height > 3){
	return false;
      }else{
	return true;
      }
    }
    function validateWeight($weight){
      if($weight < 1 && $weight > 500){
	return false;
      }else{
	return true;
      }
    }
  
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="zajimaveeeeeee" content="Komodo Edit 8.5">
  <link rel="stylesheet" type="text/css" href="style.css">
  <title>BITWA - Fiala Dominik</title>

  </head>
  <body>
    <div id="hlavicka">  
      <h1 align="center">BITWA</h1><br>      
    </div>
    <div id="menu">
      <? include "menu.php" ?>
    </div>
    <div id="obsah">
        <form method="GET">
	
        <label for="name"> Jmeno </label>
	<input type="text" id="name" name="name" value="<?php echo $_GET['name'];?>">
	
	<label for="password"> Heslo </label>
	<input type="password" id="password" name="password" value="<?php echo $_GET['password'];?>">
        
	<br/>
	<label for="height"> Vyska (cm) </label>
	<input type="text" id="height" name="height" value="<?php echo $_GET['height'];?>">
        
	<br/>
	<label for="weight"> Vaha (kg) </label>
	<input type="text" id="weight" name="weight" value="<?php echo $_GET['weight'];?>">
        
	<input type="submit" name="save" value="save">
        </form>
      <br><b>
    <?php
        if(empty($_GET[name]))
	  $report[name] = "Vyplnte jmeno";
	if(!is_numeric($_GET[height]) || !validateHeight($_GET[height]/100))
          $report[height] = "Chyba ve vysce";
        if(!is_numeric($_GET[weight]) || !validateWeight($_GET[weight]))
          $report[weight] = "Chyba ve vaze";
	  
        if(empty($report) && ($_GET['save'])=="save"){
	  $a = "INSERT INTO `users_bmi_cv4` ( `name` , `password` )VALUES ('$_GET[name]', '$_GET[password]')";
	  $res = mysql_query($a);
	  if(!$res){
	    $report[sql1] = "Chyba v SQL:".mysql_error();
	  }
	 
	  $b = "SELECT `id` FROM `users_bmi_cv4` ORDER BY `id` DESC LIMIT 1";
	  $res1 = mysql_query($b);
	  if(!$res1){
	    $report[sql2] = "Chyba v SQL:".mysql_error();  
	  }else{
	    $row = mysql_fetch_assoc($res1);
	    $user_id = $row[id];
	  }
	  
	  $_GET[height] = $_GET[height]/100;
	  $bmi = $_GET[weight] / ($_GET[height]*$_GET[height]) ;
	  
	  $c = "INSERT INTO `data_bmi_cv4` ( `user_id` , `height` , `weight` , `bmi` )
	  VALUES ('$user_id', '$_GET[height]', '$_GET[weight]', '$bmi')";
	  $res2 = mysql_query($c);
	  if(!$res2)
	    $report[sql3] = "Chyba v SQL:".mysql_error();
	    
	  $d = "SELECT * FROM `data_bmi_cv4`";
	  $res4 = mysql_query($d);
	  if(!$res4){
	    $report[sql3] = "Chyba v SQL:".mysql_error();
	  }else{
	    echo "<table>";
	    while($row = mysql_fetch_assoc($res4)){
	      $napis = vyhodnot($row['bmi']);
	      echo "<tr><td>".$row['user_id']."</td>
			<td>".$row['height']."</td>
			<td>".$row['weight']."</td>
			<td>".$row['bmi']."</td>
			<td>".$napis."</td></tr>";
	    }
	    echo "</table>";
	  }
	  mysql_close($link);
	  
        }else{
	  foreach($report as $chyba){
            echo "$chyba<br>";
	  }
        }
    ?>
    </b>
    </div>
    <div id="copyright">
      &copy fialadom
    </div>  
  </body>
</html>